package com.liuzy.crawler.core;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

/**
 * @author liuzy
 * @since 2015年9月24日
 */
public class MyListener {

	public static void addNumberVerifyListener(final Text txt, final int start, final int end) {
		addNumberListener(txt);
		txt.addListener(SWT.FocusOut, new Listener() {
			@Override
			public void handleEvent(Event e) {
				Integer v = null;
				try {
					v = Integer.parseInt(txt.getText());
					if (v < start) {
						txt.setText(start + "");
					} else if (v > end) {
						txt.setText(end + "");
					}
				} catch (Exception e2) {
					txt.setText(end + "");
				}
			}
		});
	}

	private static void addNumberListener(final Text txt) {
		txt.addVerifyListener(new VerifyListener() {
			@Override
			public void verifyText(VerifyEvent e) {
				if ("".equals(e.text))
					return;
				try {
					Integer.parseInt(e.text);
				} catch (Exception e1) {
					e.doit = false;
				}
				return;
			}
		});
	}

}
